<?php
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 *
 * Author: YSX
 * Date: 2023/04/05
 */
namespace app\admin\controller;

use app\common\controller\Backend;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
class Event extends Backend{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index(){
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $limit = $this->request->post('limit') ? $this->request->post('limit') : 15;

            $condition = [];
            if (!empty($keys)) {
                $condition[] = ['mobile', 'like', '%' . $keys . '%'];
            }
            $list = Db::name('event')
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->toArray();

            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }
        return view();

    }

    public function add(){
        if (Request::isPost()) {
            $params = $this->request->post();
   			$data = [
   				'name'=>$params['name'],
   				'addtime'=>time(),
   			];
            $res = Db::name('event')->insert($data);
            if ($res) {
                $this->success(lang('add success'),url('index'));
            } else {
                $this->error(lang('add fail'));
            }
        }
        $view = [
            'info' => '',
            'title' => lang('add'),
        ];
        View::assign($view);
        return view();
    }

    public function edit(){
    	$id = input('id');
        if (Request::isPost()) {
        	$params = $this->request->post();
            $data = [
   				'name'=>$params['name'],
   				// 'addtime'=>time(),
   			];
            $res = Db::name('event')->where('id',$id)->update($data);
            if ($res) {
                $this->success(lang('edit success'), url('index'));
            } else {
                $this->error(lang('edit fail'));
            }
        }
        $info = Db::name('event')->where('id',$id)->find();
        $view = [
            'info' => $info,
            'title' => '修改',
        ];
        View::assign($view);
        return view('add');
    }
	//修改状态
	public function state(){
		$id = input('id');
		$info = Db::name('event')->where('id',$id)->find();
		$data['status'] = $info['status'] == 1 ? 2 : 1;
		$res = Db::name('event')->where('id',$id)->update($data);
		if ($res) {
		    $this->success(lang('edit success'), url('index'));
		} else {
		    $this->error(lang('edit fail'));
		}
	}
}